package com.ssly.media.manage.ffmpeg;

import com.ssly.media.manage.util.Helper;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class MediaConverter implements Runnable{

    private final static String TIME_REGEX = "time=([^(,+})\\s]*)";
    //    转码进度
    private float process;

    //输入目录
    private Media input;
    //输出目录
    private Media output;

    public MediaConverter(Media input, Media output) {
        this.input = input;
        this.output = output;
    }


    public static void main(String[] args) {
        Media input = new Media("E:\\XMPCache", "星球大战8：最后的绝地武士BD中英双字[飘花www.piaohua.com].mp4");
        Media output = new Media("D:\\MyPath\\1222111.ts");
        output.setVcodec("h264");

        MediaConverter mediaConverter = new MediaConverter(input, output);
        mediaConverter.run();
    }

    @Override
    public void run() {
        List<String> command = new ArrayList<>();
        command.add("ffmpeg");
        command.add("-i");
        command.add(input.getFilePath());
        if (null != output.getVcodec()) {
            command.add("-vcodec");
            command.add(output.getVcodec());
        }
        if (null != output.getAcodec()) {
            command.add("-acodec");
            command.add(output.getAcodec());
        }
        if (null != output.getWidth() && null != output.getHeight()) {
            command.add("-s");
            command.add(output.getWidth().toString() + 'x' + output.getHeight().toString());
        }
        command.add(output.getFilePath());
        command.add("-y");


        System.out.println(command);
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        processBuilder.redirectErrorStream(true);
        try {
            Process process = processBuilder.start();
            InputStream inputStream = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            String line;
            while ((line = br.readLine()) != null) {
                String time = Helper.stringExtract(TIME_REGEX, line);
                if (null != time) {
                    System.out.println(time);
                    Integer curLenght = Media.getLenght(time);
                    Integer allLenght = input.getLenght();
                    this.process = (float) (curLenght * 100.0 / allLenght);
                    System.out.println(this.process);
                }
            }
            int exitVal = process.waitFor();
            System.out.println("Process exitValue: " + exitVal);
            if (exitVal==1){
                System.out.println("错误退出");
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void stop() {

    }

    public void successHandler() {

    }

    public void failureHandler() {

    }

    public void getMediaDuration() {

    }

    public float getProcess() {
        return process;
    }

    public void setProcess(float process) {
        this.process = process;
    }

}
